import { deviceManage, deviceSearch, selectPolicy, selectSite } from '@/api/vm'

const store = {
  namespaced: true,
  state: {
    tableLists: [],
    tableDatas: {},
    page: 1,
    policyList: [],
    site: []
  },
  getters: {

  },
  mutations: {
    setDeviceManage (state, payload) {
      payload.currentPageRecords.forEach(item => {
        if (item.vmStatus === 0) {
          item.vmStatus = '未投放'
        } else if (item.vmStatus === 1) {
          item.vmStatus = '运营'
        } else if (item.vmStatus === 3) {
          item.vmStatus = '撤机'
        }
      })
      state.tableLists = payload.currentPageRecords
      state.tableDatas = payload
    },

    setPage (state, payload) {
      state.page = payload
    },
    setDviceSearch (state, payload) {
      state.tableLists = payload.currentPageRecords
    },
    setSelectPolicy (state, payload) {
      state.policyList = payload
    },
    setSite (state, payload) {
      state.site = payload.currentPageRecords
    }
  },
  actions: {
    async getDeviceManage (context, payload = 1) {
      const res = await deviceManage(payload)
      context.commit('setDeviceManage', res)
    },
    async getDeviceSearch (context, payload) {
      const res = await deviceSearch(payload)
      console.log(res)
      context.commit('setDviceSearch', res)
    },
    async getSelectPolicy (context) {
      const res = await selectPolicy()
      console.log(res)
      context.commit('setSelectPolicy', res)
    },
    async getSite (context) {
      const res = await selectSite()
      console.log(res)
      context.commit('setSite', res)
    }
  }
}

export default store
